package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(path = "/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids) {
        String[] ids_split = ids.split(",");
        for (String id : ids_split) {
            userService.delete(Long.valueOf(id));
        }
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Long id){
        System.out.println(1111111);
        User user = userService.getUserById(id.intValue());
        return Result.success(user);
    }

    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAllUsers() {
        List<User> users = userService.getAllUsers();
        return Result.success(users);
    }

    @GetMapping("/role/{roleLabel}")
    public Result getUsersByRole(@PathVariable String roleLabel) {
        List<User> users = userService.getUsersByRole(roleLabel);
        return Result.success(users);
    }

    @GetMapping("/dept/{deptId}")
    public Result getUsersByDept(@PathVariable Integer deptId) {
        List<User> users = userService.getUsersByDept(deptId);
        return Result.success(users);
    }
}